﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using MET.Web.Models;
using Management.Entity;
using System.ComponentModel;
using Management.ViewModel;
using Management.BLL;
using Management.Tools;


namespace MET.Web.Controllers
{
    public class LoginController : BaseController
    {
        //private METDBContext db = new METDBContext();

        //
        // GET: /Login/
        [Anonymous]
        public ActionResult Index()
        {
            return View();
        }
        //[HttpPost]
        //[Anonymous]
        //public ActionResult Login(Login logininfo)
        //{
        //    try
        //    {
        //        if (string.IsNullOrEmpty(logininfo.LoginName) || string.IsNullOrEmpty(logininfo.LoginPassword))
        //        {
        //            return Content("用户名密码不能为空！");
        //        }
        //        else
        //        {
        //            var v = (from q in db.Login
        //                     where q.LoginName == logininfo.LoginName
        //                     && q.LoginPassword == logininfo.LoginPassword
        //                     select q).FirstOrDefault();
        //            if (v != null)
        //            {
        //                Session["UserID"] = v.Id;
        //                return Content("OK");
        //            }
        //            else
        //            {
        //                return Content("用户名密码错误!");
        //            }
        //        }
        //    }
        //    catch (Exception)
        //    {
                
        //        throw;
        //    }
            
        //}
        [Description("login登录,登录成功则跳转")]
        [Anonymous]
        [UIExceptionResult]
        public ActionResult Login()
        {
            UserRequest request = new UserRequest(HttpContext);
            UserService service = new UserService();
            var user = service.Login(request.UserName, request.Password);
            if (user != null)
            {
                CurrentUserHelper.SaveUserInfo(user);
                UserOperateLogHelper.WriteOperateLog("[用户登录] 登陆成功");
                SessionHelper.SetSession("UserID", user.UID);
                //读取角色信息
                string UserRoles = service.GetUserAllRole(user.UID);
                SessionHelper.SetSession("UserRoles", UserRoles);
                //设置前台显示的昵称信息
                SessionHelper.SetSession("Title", user.NickName);
                SessionHelper.SetSession("CurrentUserLastLoginTime", user.LastLoginTime);
                return Content("OK");
            }
            else
            {
                return Content("Error");
            }
        }
    }
}
